<?php
require_once PATH_SITE.'cn/royan/php/values/ServerSocket.class.php';
require_once PATH_SITE.'cn/royan/php/utils/builders/BuilderRoom.builder.php';
require_once PATH_SITE.'cn/royan/php/utils/builders/BuilderArea.builder.php';
require_once PATH_SITE.'cn/royan/php/utils/builders/Builder.class.php';
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BuilderSocket
 *
 * @author yanr
 */
class BuilderSocket extends Builder
{
    //put your code here
    public function __construct( $value ) {
        $this->config = $value;
    }
    
    public function build() {
        $this->obj = new ServerSocket();
        
        $areas = $this->config["config"]->__get("areas");
        $arealist = array();
        IF( count( $areas ) > 0 )
        {
            for( $i = 0; $i < count( $areas ); $i++ )
            {
                $info = array( "name"=>$areas[$i]->name );
                $builderArea = new BuilderArea( $info );
                $area = $builderArea->build()->getResult();
                array_push( $arealist, $area );
                
                $rooms = $areas[$i]->rooms;
                $roomlist = array();
                IF( count( $rooms ) > 0 )
                {
                    for( $j = 0; $j < count( $rooms ); $j++ ){
                        $info = array( "name"=>$rooms[$j]->name,
                                       "linkNumber"=>$rooms[$j]->linkNumber,
                                       "area"=>$area->__get("id"),
                                       "password"=>  is_null($rooms[$j]->password)?NULL:$rooms[$j]->password );
                        $builderRoom = new BuilderRoom( $info );
                        $room = $builderRoom->build()->getResult();
                        array_push( $roomlist, $room );

                        IF( $rooms[$j]->default === TRUE ) $area->__set( "default", $room );
                    }
                    
                    IF( is_null( $area->__get("default") ) )
                        $area->__set( "default", $rooms[0] );
                }
                ELSE
                {
                    $info = array( "name"=>"The Lobby",
                                   "linkNumber"=>200,
                                   "area"=>$area,
                                   "password"=> NULL );
                    $builderRoom = new BuilderRoom( $info );
                    $room = $builderRoom->build()->getResult();
                    array_push( $roomlist, $room );
                    $area->__set( "default", $room );
                }
                $area->__set( "rooms", $roomlist );
                
                IF( $areas[$i]->default === TRUE ) $this->obj->__set( "default", $area );
            }
            
            IF( is_null( $this->obj->__get("default") ) )
                $this->obj->__set( "default", $arealist[0] );
        }
        ELSE
        {
            $info = array( "name"=>"Chat" );
            $builderArea = new BuilderArea( $info );
            $area = $builderArea->build()->getResult();
            array_push( $arealist, $area );
            $this->obj->__set( "default", $area );
        }
        
        $this->obj->__set("socket",         $this->config["socket"]);
        $this->obj->__set("host",           $this->config["config"]->__get("host") );
        $this->obj->__set("port",           $this->config["config"]->__get("port") );
        $this->obj->__set("linkNumber",     $this->config["config"]->__get("linkNumber") );
        
        $this->obj->__set("connects",       array() );
        $this->obj->__set("clientInfos",    array() );
        $this->obj->__set("areas",          $arealist );
        
        RETURN parent::build();
    }
}

?>
